//
//  InFavouriteViewController.m
//  Inner  Voice
//
//  Created by 徐海宇 on 16/3/2.
//  Copyright © 2016年 徐海宇. All rights reserved.
//

#import "InFavouriteViewController.h"

#import "InFavouriteTableViewCell.h"

#import "InSound.h"

#import <AFNetworking.h>

#import "AppDelegate.h"

#define kIdentifier @"favouriteCell"

#define appDelegate ((AppDelegate *)[[UIApplication sharedApplication]delegate])
#import "InSoundTool.h"

@interface InFavouriteViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *myTableView;

@property (nonatomic,strong)NSMutableArray *favouriteArray;

@end

@implementation InFavouriteViewController

- (NSMutableArray *)favouriteArray {
    if (_favouriteArray == nil) {
        _favouriteArray = [NSMutableArray array];
    }
    return _favouriteArray;
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
   // [self loadMyFavouriteSoundWithUserid:@"6864905" withPage:1];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.myTableView registerNib:[UINib nibWithNibName:@"InFavouriteTableViewCell" bundle:nil] forCellReuseIdentifier:kIdentifier];
    
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    
  //  [userDefault setValue:@"6864905" forKey:@"u_id"];
    
    NSString *user_id = [userDefault valueForKey:@"u_id"];
    
    
    
    [self loadMyFavouriteSoundWithUserid:user_id withPage:1];
    
}
- (IBAction)backBtnClick:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (IBAction)startBtnClick:(id)sender {
    InPlayerViewController *contro = [[InPlayerViewController alloc]init];
    [self presentViewController:contro animated:YES completion:nil];
}


//网络请求
- (void)loadMyFavouriteSoundWithUserid:(NSString *)userid withPage:(int)page{
    if ([AppDelegate shareAppdelegate].canConnectedToTheInternet) {

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    NSString *url = [NSString stringWithFormat:@"http://1.wangtest11.applinzi.com/home/Banner/backuserattentionsounds?userid=%@&page=%d",userid,page];
    
    [manager GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSLog(@"%@",responseObject);
        
        NSMutableArray *responseObjectArray = responseObject[@"data"];
        
       // NSLog(@"%@",responseObjectArray[0]);
        if (responseObjectArray.count == 0) {
            self.favouriteArray = nil;
        }else {
    
        for (int i = 0; i<responseObjectArray.count; i++) {
            InSound *sound = [[InSound alloc]init];
            sound.sound_id = responseObjectArray[i][@"sound_id"];
            sound.inSound_icon = responseObjectArray[i][@"pic_100"];
            sound.inSound_name = responseObjectArray[i][@"name"];
            sound.inSound_Url = responseObjectArray[i][@"source"];
            
            [self.favouriteArray addObject:sound];
        }
    }
            [self.myTableView reloadData];
        
    
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];

}else {
    [self showAlertView:@"当前网络不可用"];
}
}
#pragma mark - 封装UIAlertView方法，直接调用就可以显示提示
-(void)showAlertView:(NSString *)string {
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:string delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [alertView show];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.favouriteArray.count == 0) {
        return 1;
    }else {
        
    return self.favouriteArray.count;
        
    }
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    InFavouriteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kIdentifier forIndexPath:indexPath];
    
    if (self.favouriteArray.count == 0) {
         cell.soundNameLabel.text = @"暂无喜欢的音乐";
    }else {
    
    InSound *sound = self.favouriteArray[indexPath.row];

    cell.soundNameLabel.text = sound.inSound_name;
    
    [cell loadImageWithStr:sound.inSound_icon];
    
  //  [cell loadImageWithStr:@"http://echo-mx.b0.upaiyun.com/1768960618.jpg!100"];
    }
    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  //  InSound *sound = self.favouriteArray[indexPath.row];
    if (self.favouriteArray.count == 0) {
        
    }else {
    InSound *sound = self.favouriteArray[indexPath.row];//得到当前点击的歌曲的信息
    NSURL *url = [NSURL URLWithString:sound.inSound_Url];
    
    [appDelegate.InPlayerViewController.inplayer playWithUrl:url showView:appDelegate.InPlayerViewController.sliderView];//播放该歌曲的信息
    
    appDelegate.InPlayerViewController.index = indexPath.row;//将歌曲的index传给播放器页面
    
    
    appDelegate.InPlayerViewController.inSound = sound;
    
    appDelegate.InPlayerViewController.playingSoundArray = self.favouriteArray;//播放器正在播放的数组为历史播放记录的数组
    
        [self dismissViewControllerAnimated:YES completion:nil];
    }

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
